/*弹出层*/
/*
	参数解释：
	title	标题
	url		请求的url
	id		需要操作的数据id
	w		弹出层宽度（缺省调默认值）
	h		弹出层高度（缺省调默认值）
*/
function layer_show(title,url,w,h){
  if (title == null || title == '') {
    title=false;
  };
  if (url == null || url == '') {
    url="404.html";
  };
  if (w == null || w == '') {
    w=($(window).width()*0.9);
  };
  if (h == null || h == '') {
    h=($(window).height() - 50);
  };
  layer.open({
    type: 2,
    area: [w+'px', h +'px'],
    fix: false, //不固定
    maxmin: true,
    shade:0.4,
    title: title,
    content: url
  });
}

function layer_show_detail(title,page,reload,w,h){
  if (title == null || title == '') {
    title=false;
  };
  if (page == null || page == '') {
    layer.alert("page参数未定义");
    return false;
  };
  if (w == null || w == '') {
    w=($(window).width()*0.9);
  };
  if (h == null || h == '') {
    h=($(window).height() - 50);
  };
  var url = getContextPath()+ '/search/commondetail.htm?page=' + page;
  layer.open({
    type: 2,
    area: [w+'px', h +'px'],
    fix: false, //不固定
    maxmin: true,
    shade:0.4,
    title: title,
    content: url,
    zIndex: layer.zIndex, //重点1
    success: function(layero){
      layer.setTop(layero); //重点2 点击某个窗口，该窗体就置顶在上面
    },
    end:function(){
      if(null!=reload&&reload==true){
        reloadTable();
      }
    }
  });
}

/*关闭弹出框口*/
function layer_close(){
  var index = parent.layer.getFrameIndex(window.name);
  parent.layer.close(index);
}

function ajaxPost(url,data){
  var result = null;
  var code = null;
  var message = null;
  $.ajax({
    url: url,
    type:'post',
    async: false,
    dateType:'json',
    beforeSend: function(xhr) {
      // xhr.setRequestHeader("token:'loginUser:0000:6fe072c8-9a15-40ae-b538-a2988feb143f'");
    },
    headers: {'Content-Type':'application/json;charset=utf8'},
    data:JSON.stringify(data),
    success:function(res){
      code = res.code;
      message = res.message;
      result = res;
    },
    error:function(jqXHR, textStatus, errorThrown){
      /*弹出jqXHR对象的信息*/
      // console.log(jqXHR.responseText);
      // console.log(jqXHR.status);
      // console.log(jqXHR.readyState);
      // console.log(jqXHR.statusText);
      /*弹出其他两个参数的信息*/
      // console.log(textStatus);
      console.log(errorThrown);
    }
  });
  if(code != null && code=='401'){
    alert(message);
    top.location.href = getContextPath()+ '/user/login';
    return false;
  }
  if(null != result &&  null != result.success  && !result.success){
    alert(result.message)
  }
  return result;
}

function addToCar(prodId,prodNum,showAlert){
  var url = getContextPath()+ '/car/addToCar';
  var data = {'prodId':prodId,'prodNum':prodNum};
  var result = ajaxPost(url,data);
  console.log(result);
  if(showAlert && null != result && null != result.success && result.success){
    alert("添加购物车成功!")
  }
}